package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listenter.SubjectExcelListenter;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-08
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            //文件输入流
            InputStream in =file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListenter(subjectService)).sheet().doRead();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //实现 多级分类
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.一级数据 parent_id 等于零
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");

        List<EduSubject> eduSubjects = baseMapper.selectList(wrapperOne);
//        List<EduSubject> list = this.list(wrapperOne);

        //2.一级数据 parent_id 不等于零
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id","0");//不等于
        List<EduSubject> eduSubjects1 = baseMapper.selectList(wrapperTwo);

        //list集合 ，用于存储最终的数据
        List<OneSubject>  finalSubjectList = new ArrayList<>();

        //封装一级分类
        for (int i = 0; i < eduSubjects.size(); i++) {

            EduSubject eduSubject = eduSubjects.get(i);
            OneSubject oneSubject= new OneSubject();

//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //等效于  要求属性名相同
            BeanUtils.copyProperties(eduSubject,oneSubject);

            finalSubjectList.add(oneSubject);


            //封装二级分类

            List<TwoSubject>  twoSubjects = new ArrayList<>();
            for (int m = 0; m < eduSubjects1.size(); m++) {
                //获取二分类
                EduSubject tSubject = eduSubjects1.get(m);
                if(tSubject.getParentId().equals(eduSubject.getId())){

                    TwoSubject twoSubject = new TwoSubject();

                    BeanUtils.copyProperties(tSubject,twoSubject);

                    twoSubjects.add(twoSubject);
                }
            }
            //彻底封装
            oneSubject.setChildren(twoSubjects);


        }



        return finalSubjectList;
    }
}
